<?php

namespace app\api\controller;

use think\Db;

class Skin extends Common {
	public function plans()
	{
		$data = Db::name('nursing_plan')->order('sort desc, id asc')->field('id,name,icon,content')->select();
		foreach ($data as &$value) {
            $value['content'] = str_replace('/disk/uploads/', _ossUrl(1) . '/disk/uploads/', $value['content']);
		}
		return $this->pass($data);
	}

	public function options()
	{
		$data = Db::name('assess_select')->order('step asc, id asc')->field('id,label,multiple')->select();

		$options = Db::name('assess_options')->order('sort desc, id asc')->field('id,select_id,label')->select();

		foreach ($data as &$value) {
			$value['options'] = [];
			foreach ($options as $v) {
				if ($v['select_id'] == $value['id']) {
					unset($v['select_id']);
					$value['options'][] = $v;
				}
			}
		}

		return $this->pass($data);
	}

	public function assessSubmit($ids = '', $age = 18)
	{

		$user_id = $this->member['id'];
		$user_id = 6;

		$options = Db::name('assess_options')->where(['id'=>['in', $ids]])->select();
		$directions = Db::name('assess_direction')->column('*', 'id');
		$directionCounts = [];

		// 现状
		$directionData = [];
		// 习惯
		$tips = [];
		// 建议
		$recommendations = [];

		foreach ($options as $key => $value) {
			if ($value['direction_id']) {
				if (!isset($directionCounts[$value['direction_id']])) {
					$directionCounts[$value['direction_id']] = 0;
				}
				$directionCounts[$value['direction_id']] += $value['direction_val'];
			}

			if ($value['tip']) {
				$tips[] = [
					'label'=>$value['tip_title'] ?: $value['label'],
					'value'=>$value['tip']
				];
			}

			if ($value['recommendation']) {
				if (!isset($recommendations[$value['recommendation_type']])) {
					$recommendations[$value['recommendation_type']] = [];
				}
				$recommendations[$value['recommendation_type']][] = $value['recommendation'];
			}
		}

		foreach ($directionCounts as $key => $value) {
			$valuekey = '';
			if ($value >= $directions[$key]['severe']) {
				$valuekey = 'severe';
			} else if ($value >= $directions[$key]['moderate']) {
				$valuekey = 'moderate';
			} else if ($value >= $directions[$key]['small']) {
				$valuekey = 'small';
			}
			$directionData[] = [
				'label'=>$directions[$key]['label'],
				'index'=>$directions[$key][$valuekey] ?? 0,
				'index_tip'=>$directions[$key][$valuekey . '_tip'] ?? '',
			];
		}

		if ($age < 26) {
			$age_desc = '轻龄活力肌肤';
		} else if ($age < 40) {
			$age_desc = '熟龄美丽肌肤';
		} else if ($age >= 40) {
			$age_desc = '深度熟龄肌肤';
		}

		$data['directions'] = $directionData;
		$data['tips'] = $tips;
		$data['recommendations'] = $recommendations;
		$data['age'] = $age;
		$data['age_desc'] = $age_desc;

		Db::name('assess_records')->insert(['user_id'=>$user_id,'data'=>json_encode($data, JSON_UNESCAPED_UNICODE), 'create_time'=>time()]);

		return $this->pass($data);
	}

	public function assessRecords($page = 1, $pagesize = 20)
	{
		$user_id = $this->member['id'];
		$user_id = 6;
		$data = Db::name('assess_records')
					->where(['user_id'=>$user_id])
					->page($page)
					->limit($pagesize)
					->field('id,create_time')
					->order('id desc')
					->select();
		foreach ($data as &$value) {
			$value['label']	= '问卷评测';
			$value['create_time'] = _time($value['create_time']);
		}
		return $this->pass($data);
	}

	public function assessInfo($id)
	{
		$user_id = $this->member['id'];
		$user_id = 6;
		$data = Db::name('assess_records')
					->where(['id'=>$id,'user_id'=>$user_id])
					->find();
		$data['data'] = json_decode($data['data'], true);
		return $this->pass($data['data']);
	}
}